﻿using System;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using System.Text;

using DotNetWarcraft.Common;
using DotNetWarcraft.WoWGuildRoster.Business;
using DotNetWarcraft.WoWGuildRoster.Business.Exceptions;
using Microsoft.ApplicationBlocks.Data;

namespace DotNetWarcraft.WoWGuildRoster.Data {

  public class SqlGuildRosteryRepository : SqlDataProvider, IRepository<GuildRoster> {

    #region IRepository<GuildRoster> Members
    public GuildRoster[] All() {
      String _methodName = this.ClassName + ".All()";

      this.CheckConnectionString(_methodName);

      List<GuildRoster> _rosters = new List<GuildRoster>();

      DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, FullyQualifiedName("DNW_WGR_GetRosters"));

      if (_ds.Tables.Count <= 0) {
        //No tables returned? - WTF?
        throw new ApplicationException(_methodName + " - No Tables Returned");
      } else if (_ds.Tables[0].Rows.Count <= 0) {
        //No rows were returned; no rosters to process
      } else {
        //Okay, let's get to work - we have a table and rosters to read

        foreach (DataRow _dr in _ds.Tables[0].Rows) {
          GuildRoster _roster = new GuildRoster(_dr);
          _rosters.Add(_roster);
        }
      }
      return _rosters.ToArray<GuildRoster>();
    }

    public GuildRoster[] FindAll(Func<GuildRoster, bool> exp) {
      throw new NotImplementedException();
    }

    public GuildRoster Find(int rosterId) {
      String _methodName = this.ClassName + ".Find(" + rosterId.ToString() + ")";

      this.CheckConnectionString(_methodName);

      try {
        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, FullyQualifiedName("DNW_WGR_GetRoster_ByRosterID"), rosterId);

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException(_methodName + " - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no entry to process
          return new GuildRoster();
        } else if (_ds.Tables[0].Rows.Count > 1) {
          //Too many rows returned; log the error to the database and return the first one
          //
          //TODO - Uncomment this out when add in exception logging
          //
          //(new Business.Exceptions.GuildCharEntryError(new Exception(
          //  _methodName + " - Too Many Rows Returned"),
          //  -1, -1, entryId,
          //  _methodName + " - " + _ds.Tables[0].Rows.Count.ToString() + " Rows Returned")).WriteExceptionToDB();
          return new GuildRoster(_ds.Tables[0].Rows[0]);
        } else {
          return new GuildRoster(_ds.Tables[0].Rows[0]);
        }

      } catch (Exception e) {
        //
        //TODO - Uncomment this out when add in exception logging
        //
        //(new Business.Exceptions.GuildCharEntryError(e, -1, -1, entryId, _methodName)).WriteExceptionToDB();
        return null;
      }
    }

    public void Delete(GuildRoster roster) {
      if (roster == null) {
        return;
      }

      int _rosterId = roster.Roster_ID;

      String _methodName = this.ClassName + ".Delete(" + _rosterId.ToString() + ")";

      if (_rosterId < 1) { return; }
      this.CheckConnectionString(_methodName);

      try {
        int update = SqlHelper.ExecuteNonQuery(ConnectionString, FullyQualifiedName("DNW_WGR_DeleteRoster"), _rosterId);
        if (update > 0) {
          return;
        } else {
          return;
        }
      } catch (Exception e) {
        //
        //TODO - Uncomment this out when add in exception logging
        //
        //(new Business.Exceptions.GuildCharEntryError(e, -1, -1, entryId, _methodName)).WriteExceptionToDB();
        return;
      }
    }

    public int Save(GuildRoster roster) {
      if (roster == null) {
        return -1;
      }
      int _rosterId = roster.Roster_ID;

      String _methodName = this.ClassName + ".Save(" + _rosterId.ToString() + ")";

      this.CheckConnectionString(_methodName);
      try {
        if (_rosterId < 1) {
          //Existing entity - add
          return (int)SqlHelper.ExecuteScalar(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_AddRoster",
            roster.Guild_Name,
            roster.Guild_Server,
            roster.Guild_Region,
            SqlDataUtils.ValidateDbValue(roster.GuildRank0),
            SqlDataUtils.ValidateDbValue(roster.GuildRank1),
            SqlDataUtils.ValidateDbValue(roster.GuildRank2),
            SqlDataUtils.ValidateDbValue(roster.GuildRank3),
            SqlDataUtils.ValidateDbValue(roster.GuildRank4),
            SqlDataUtils.ValidateDbValue(roster.GuildRank5),
            SqlDataUtils.ValidateDbValue(roster.GuildRank6),
            SqlDataUtils.ValidateDbValue(roster.GuildRank7),
            SqlDataUtils.ValidateDbValue(roster.GuildRank8),
            SqlDataUtils.ValidateDbValue(roster.GuildRank9),
            SqlDataUtils.ValidateDbValue(roster.GuildRank10),
            SqlDataUtils.ValidateDbValue(roster.Guild_Alts_To),
            SqlDataUtils.ValidateDbValue(roster.Guild_Graphic),
            SqlDataUtils.ValidateDbValue(roster.Last_Load_Date),
            SqlDataUtils.ValidateDbValue(roster.Last_Load_ID),
            SqlDataUtils.ValidateDbValue(roster.Last_Update_Date),
            SqlDataUtils.ValidateDbValue(roster.Last_Update_By));
        } else {
          //Existing entity - update
          int update = SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_UpdateRoster",
            roster.Roster_ID,
            roster.Guild_Name,
            roster.Guild_Server,
            roster.Guild_Region,
            SqlDataUtils.ValidateDbValue(roster.GuildRank0),
            SqlDataUtils.ValidateDbValue(roster.GuildRank1),
            SqlDataUtils.ValidateDbValue(roster.GuildRank2),
            SqlDataUtils.ValidateDbValue(roster.GuildRank3),
            SqlDataUtils.ValidateDbValue(roster.GuildRank4),
            SqlDataUtils.ValidateDbValue(roster.GuildRank5),
            SqlDataUtils.ValidateDbValue(roster.GuildRank6),
            SqlDataUtils.ValidateDbValue(roster.GuildRank7),
            SqlDataUtils.ValidateDbValue(roster.GuildRank8),
            SqlDataUtils.ValidateDbValue(roster.GuildRank9),
            SqlDataUtils.ValidateDbValue(roster.GuildRank10),
            SqlDataUtils.ValidateDbValue(roster.Guild_Alts_To),
            SqlDataUtils.ValidateDbValue(roster.Guild_Graphic),
            SqlDataUtils.ValidateDbValue(roster.Last_Load_Date),
            SqlDataUtils.ValidateDbValue(roster.Last_Load_ID),
            SqlDataUtils.ValidateDbValue(roster.Last_Update_Date),
            SqlDataUtils.ValidateDbValue(roster.Last_Update_By));

          return roster.Roster_ID;
        }
      } catch (Exception eExc) {
        throw eExc;
      }
    }
    #endregion
  }

  public class SqlGuildEntryRepository : SqlDataProvider, IRepository<GuildEntry> {

    #region IRepository<GuildEntry> Members
    public GuildEntry[] All() {
      String _methodName = this.ClassName + ".All()";

      this.CheckConnectionString(_methodName);

      List<GuildEntry> _entries = new List<GuildEntry>();

      DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, FullyQualifiedName("DNW_WGR_GetCharEntries_All"));

      if (_ds.Tables.Count <= 0) {
        //No tables returned? - WTF?
        throw new ApplicationException(_methodName + " - No Tables Returned");
      } else if (_ds.Tables[0].Rows.Count <= 0) {
        //No rows were returned; no rosters to process
      } else {
        //Okay, let's get to work - we have a table and rosters to read

        foreach (DataRow _dr in _ds.Tables[0].Rows) {
          GuildEntry _entry = new GuildEntry(_dr);
          _entries.Add(_entry);
        }
      }
      return _entries.ToArray<GuildEntry>();
    }

    public GuildEntry[] FindAll(Func<GuildEntry, bool> exp) {
      throw new NotImplementedException();
    }

    public GuildEntry Find(int entryId) {
      String _methodName = this.ClassName + ".Find(" + entryId.ToString() + ")";

      this.CheckConnectionString(_methodName);

      try {
        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, FullyQualifiedName("DNW_WGR_GetCharEntry_ByEntryID"), entryId);

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException(_methodName + " - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no entry to process
          return new GuildEntry();
        } else if (_ds.Tables[0].Rows.Count > 1) {
          //Too many rows returned; log the error to the database and return the first one
          //
          //TODO - Uncomment this out when add in exception logging
          //
          //(new Business.Exceptions.GuildCharEntryError(new Exception(
          //  _methodName + " - Too Many Rows Returned"),
          //  -1, -1, entryId,
          //  _methodName + " - " + _ds.Tables[0].Rows.Count.ToString() + " Rows Returned")).WriteExceptionToDB();
          return new GuildEntry(_ds.Tables[0].Rows[0]);
        } else {
          //Okay, let's get to work - we have a table and roster to read
          return new GuildEntry(_ds.Tables[0].Rows[0]);
        }

      } catch (Exception e) {
        //
        //TODO - Uncomment this out when add in exception logging
        //
        //(new Business.Exceptions.GuildCharEntryError(e, -1, -1, entryId, _methodName)).WriteExceptionToDB();
        return null;
      }
    }

    public void Delete(GuildEntry entry) {
      if (entry == null) {
        return;
      }

      int _entryId = entry.Char_ID;

      String _methodName = this.ClassName + ".Delete(" + _entryId.ToString() + ")";

      if (_entryId < 1) { return; }
      this.CheckConnectionString(_methodName);

      try {
        int update = SqlHelper.ExecuteNonQuery(ConnectionString, FullyQualifiedName("DNW_WGR_DeleteCharEntry"), _entryId);
        if (update > 0) {
          return;
        } else {
          return;
        }
      } catch (Exception e) {
        //
        //TODO - Uncomment this out when add in exception logging
        //
        //(new Business.Exceptions.GuildCharEntryError(e, -1, -1, entryId, _methodName)).WriteExceptionToDB();
        return;
      }
    }

    public int Save(GuildEntry entry) {
      if (entry == null) {
        return -1;
      }
      int _entryId = entry.Char_ID;

      String _methodName = this.ClassName + ".Save(" + _entryId.ToString() + ")";

      this.CheckConnectionString(_methodName);
      try {
        if (_entryId < 1) {
          //Existing entity - add
          return (int)SqlHelper.ExecuteScalar(ConnectionString, FullyQualifiedName("DNW_WGR_AddCharEntry"),
            SqlDataUtils.ValidateDbValue(entry.Roster_ID),
            SqlDataUtils.ValidateDbValue(entry.Char_Server),
            SqlDataUtils.ValidateDbValue(entry.Char_Guild),
            SqlDataUtils.ValidateDbValue(entry.Char_Region),
            SqlDataUtils.ValidateDbValue(entry.LoadStatus),
            SqlDataUtils.ValidateDbValue(entry.Current_Flag),
            SqlDataUtils.ValidateDbValue(entry.Char_Name),
            SqlDataUtils.ValidateDbValue(entry.Char_Race),
            SqlDataUtils.ValidateDbValue(entry.Char_Gender),
            SqlDataUtils.ValidateDbValue(entry.Char_Class),
            SqlDataUtils.ValidateDbValue(entry.Char_Level),
            SqlDataUtils.ValidateDbValue(entry.Game_Last_Login),
            SqlDataUtils.ValidateDbValue(entry.Char_Spec_Num),
            SqlDataUtils.ValidateDbValue(entry.Char_Spec),
            SqlDataUtils.ValidateDbValue(entry.Char_Spec1),
            SqlDataUtils.ValidateDbValue(entry.Char_Spec1_Level),
            SqlDataUtils.ValidateDbValue(entry.Char_Spec2),
            SqlDataUtils.ValidateDbValue(entry.Char_Spec2_Level),
            SqlDataUtils.ValidateDbValue(entry.Char_Spec3),
            SqlDataUtils.ValidateDbValue(entry.Char_Spec3_Level),
            SqlDataUtils.ValidateDbValue(entry.Char_Prof1),
            SqlDataUtils.ValidateDbValue(entry.Char_Prof1_Level),
            SqlDataUtils.ValidateDbValue(entry.Char_Prof2),
            SqlDataUtils.ValidateDbValue(entry.Char_Prof2_Level),
            SqlDataUtils.ValidateDbValue(entry.Game_Guild_Rank),
            SqlDataUtils.ValidateDbValue(entry.Game_Guild_Notes),
            SqlDataUtils.ValidateDbValue(entry.Game_Officer_Notes),
            SqlDataUtils.ValidateDbValue(entry.Primary_Char_Flag),
            SqlDataUtils.ValidateDbValue(entry.Alt_to_Main_ID),
            SqlDataUtils.ValidateDbValue(entry.Last_Load_Date),
            SqlDataUtils.ValidateDbValue(entry.Last_Load_ID),
            SqlDataUtils.ValidateDbValue(entry.Last_Update_Date),
            SqlDataUtils.ValidateDbValue(entry.Last_Update_By));
        } else {
          //Existing entity - update
          int update = SqlHelper.ExecuteNonQuery(ConnectionString, FullyQualifiedName("DNW_WGR_UpdateCharEntry"),
            entry.Char_ID,
            SqlDataUtils.ValidateDbValue(entry.Roster_ID),
            SqlDataUtils.ValidateDbValue(entry.Char_Server),
            SqlDataUtils.ValidateDbValue(entry.Char_Guild),
            SqlDataUtils.ValidateDbValue(entry.Char_Region),
            SqlDataUtils.ValidateDbValue(entry.LoadStatus),
            SqlDataUtils.ValidateDbValue(entry.Current_Flag),
            SqlDataUtils.ValidateDbValue(entry.Char_Name),
            SqlDataUtils.ValidateDbValue(entry.Char_Race),
            SqlDataUtils.ValidateDbValue(entry.Char_Gender),
            SqlDataUtils.ValidateDbValue(entry.Char_Class),
            SqlDataUtils.ValidateDbValue(entry.Char_Level),
            SqlDataUtils.ValidateDbValue(entry.Game_Last_Login),
            SqlDataUtils.ValidateDbValue(entry.Char_Spec_Num),
            SqlDataUtils.ValidateDbValue(entry.Char_Spec),
            SqlDataUtils.ValidateDbValue(entry.Char_Spec1),
            SqlDataUtils.ValidateDbValue(entry.Char_Spec1_Level),
            SqlDataUtils.ValidateDbValue(entry.Char_Spec2),
            SqlDataUtils.ValidateDbValue(entry.Char_Spec2_Level),
            SqlDataUtils.ValidateDbValue(entry.Char_Spec3),
            SqlDataUtils.ValidateDbValue(entry.Char_Spec3_Level),
            SqlDataUtils.ValidateDbValue(entry.Char_Prof1),
            SqlDataUtils.ValidateDbValue(entry.Char_Prof1_Level),
            SqlDataUtils.ValidateDbValue(entry.Char_Prof2),
            SqlDataUtils.ValidateDbValue(entry.Char_Prof2_Level),
            SqlDataUtils.ValidateDbValue(entry.Game_Guild_Rank),
            SqlDataUtils.ValidateDbValue(entry.Game_Guild_Notes),
            SqlDataUtils.ValidateDbValue(entry.Game_Officer_Notes),
            SqlDataUtils.ValidateDbValue(entry.Primary_Char_Flag),
            SqlDataUtils.ValidateDbValue(entry.Alt_to_Main_ID),
            SqlDataUtils.ValidateDbValue(entry.Last_Load_Date),
            SqlDataUtils.ValidateDbValue(entry.Last_Load_ID),
            SqlDataUtils.ValidateDbValue(entry.Last_Update_Date),
            SqlDataUtils.ValidateDbValue(entry.Last_Update_By));
          return entry.Char_ID;
        }
      } catch (Exception eExc) {
        throw eExc;
      }
    }
    #endregion

    public GuildEntry[] All_ByLevel() {
      return this.All().OrderByDescending(n => n.Char_Level).ThenBy(n => n.Char_Name).ToArray<GuildEntry>();
    }

    public GuildEntry[] All(int rosterId) {
      String _methodName = this.ClassName + ".All(" + rosterId.ToString() + ")";

      this.CheckConnectionString(_methodName);

      List<GuildEntry> _entries = new List<GuildEntry>();

      DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, FullyQualifiedName("DNW_WGR_GetCharEntries_ByRoster_AllCurrent"), rosterId);

      if (_ds.Tables.Count <= 0) {
        //No tables returned? - WTF?
        throw new ApplicationException(_methodName + " - No Tables Returned");
      } else if (_ds.Tables[0].Rows.Count <= 0) {
        //No rows were returned; no rosters to process
      } else {
        //Okay, let's get to work - we have a table and rosters to read

        foreach (DataRow _dr in _ds.Tables[0].Rows) {
          GuildEntry _entry = new GuildEntry(_dr);
          _entries.Add(_entry);
        }
      }
      return _entries.ToArray<GuildEntry>();
    }

    public GuildEntry[] All_ByLevel(int rosterId) {
      return this.All(rosterId).OrderByDescending(n => n.Char_Level).ThenBy(n => n.Char_Name).ToArray<GuildEntry>();
    }

  }

  public class SqlGuildEntryStatRepository : SqlDataProvider, IRepository<GuildEntryStat>, IGuildEntryStatRepository<GuildEntryStat> {

    #region IRepository<GuildEntryStat> Members
    public GuildEntryStat[] All() {
      String _methodName = this.ClassName + ".All()";

      this.CheckConnectionString(_methodName);

      List<GuildEntryStat> _stats = new List<GuildEntryStat>();

      DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, FullyQualifiedName("DNW_WGR_GetCharEntryStats"));

      if (_ds.Tables.Count <= 0) {
        //No tables returned? - WTF?
        throw new ApplicationException(_methodName + " - No Tables Returned");
      } else if (_ds.Tables[0].Rows.Count <= 0) {
        //No rows were returned; no rosters to process
      } else {
        //Okay, let's get to work - we have a table and rosters to read

        foreach (DataRow _dr in _ds.Tables[0].Rows) {
          GuildEntryStat _entry = new GuildEntryStat(_dr);
          _stats.Add(_entry);
        }
      }
      return _stats.ToArray<GuildEntryStat>();
    }

    public GuildEntryStat[] FindAll(Func<GuildEntryStat, bool> exp) {
      throw new NotImplementedException();
    }

    public GuildEntryStat Find(int statId) {
      String _methodName = this.ClassName + ".Find(" + statId.ToString() + ")";

      this.CheckConnectionString(_methodName);

      try {
        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, FullyQualifiedName("DNW_WGR_GetCharEntryStat"), statId);

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException(_methodName + " - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no entry to process
          return new GuildEntryStat();
        } else if (_ds.Tables[0].Rows.Count > 1) {
          //Too many rows returned; log the error to the database and return the first one
          //
          //TODO - Uncomment this out when add in exception logging
          //
          //(new Business.Exceptions.GuildCharEntryError(new Exception(
          //  _methodName + " - Too Many Rows Returned"),
          //  -1, -1, entryId,
          //  _methodName + " - " + _ds.Tables[0].Rows.Count.ToString() + " Rows Returned")).WriteExceptionToDB();
          return new GuildEntryStat(_ds.Tables[0].Rows[0]);
        } else {
          return new GuildEntryStat(_ds.Tables[0].Rows[0]);
        }

      } catch (Exception e) {
        //
        //TODO - Uncomment this out when add in exception logging
        //
        //(new Business.Exceptions.GuildCharEntryError(e, -1, -1, entryId, _methodName)).WriteExceptionToDB();
        return null;
      }
    }

    public void Delete(GuildEntryStat stat) {
      if (stat == null) {
        return;
      }

      int _statId = stat.CharStat_ID;

      String _methodName = this.ClassName + ".Delete(" + _statId.ToString() + ")";

      if (_statId < 1) { return; }
      this.CheckConnectionString(_methodName);

      try {
        int update = SqlHelper.ExecuteNonQuery(ConnectionString, FullyQualifiedName("DNW_WGR_DeleteCharEntryStat"), _statId);
        if (update > 0) {
          return;
        } else {
          return;
        }
      } catch (Exception e) {
        //
        //TODO - Uncomment this out when add in exception logging
        //
        //(new Business.Exceptions.GuildCharEntryError(e, -1, -1, entryId, _methodName)).WriteExceptionToDB();
        return;
      }
    }

    public int Save(GuildEntryStat stat) {
      if (stat == null) {
        return -1;
      }
      int _statId = stat.Char_ID;

      String _methodName = this.ClassName + ".Save(" + _statId.ToString() + ")";

      this.CheckConnectionString(_methodName);
      try {
        if (_statId < 1) {
          //Existing entity - add
          return (int)SqlHelper.ExecuteScalar(ConnectionString, FullyQualifiedName("DNW_WGR_AddUpdateCharEntryStat"),
            SqlDataUtils.ValidateDbValue(stat.Char_ID),
            SqlDataUtils.ValidateDbValue(stat.Stat_Type),
            SqlDataUtils.ValidateDbValue(stat.Stat_Group),
            SqlDataUtils.ValidateDbValue(stat.Stat_Entry),
            SqlDataUtils.ValidateDbValue(stat.Stat_Value_int),
            SqlDataUtils.ValidateDbValue(stat.Stat_Value_dec),
            SqlDataUtils.ValidateDbValue(stat.Stat_Value_str),
            SqlDataUtils.ValidateDbValue(stat.Last_Load_Date),
            SqlDataUtils.ValidateDbValue(stat.Last_Load_ID));

        } else {
          //Note - stats don't get updated
          return -1;
        }
      } catch (Exception eExc) {
        throw eExc;
      }
    }
    #endregion

    public GuildEntryStat[] Find_ByChar(int charId) {
      String _methodName = this.ClassName + ".All(" + charId + ")";

      this.CheckConnectionString(_methodName);

      List<GuildEntryStat> _stats = new List<GuildEntryStat>();

      DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, FullyQualifiedName("DNW_WGR_GetCharEntryStats_ByCharEntry"), charId);

      if (_ds.Tables.Count <= 0) {
        //No tables returned? - WTF?
        throw new ApplicationException(_methodName + " - No Tables Returned");
      } else if (_ds.Tables[0].Rows.Count <= 0) {
        //No rows were returned; no rosters to process
      } else {
        //Okay, let's get to work - we have a table and rosters to read

        foreach (DataRow _dr in _ds.Tables[0].Rows) {
          GuildEntryStat _entry = new GuildEntryStat(_dr);
          _stats.Add(_entry);
        }
      }
      return _stats.ToArray<GuildEntryStat>();
    }
    public GuildEntryStat[] Find_ByEntryAndType(String entryName, int statType) {
      String _methodName = this.ClassName + ".All(" + entryName + ", " + statType.ToString() + ")";

      this.CheckConnectionString(_methodName);

      List<GuildEntryStat> _stats = new List<GuildEntryStat>();

      DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, FullyQualifiedName("DNW_WGR_GetCharEntryStats_ByStatEntryAndType"), entryName, statType, 0);

      if (_ds.Tables.Count <= 0) {
        //No tables returned? - WTF?
        throw new ApplicationException(_methodName + " - No Tables Returned");
      } else if (_ds.Tables[0].Rows.Count <= 0) {
        //No rows were returned; no stats to process
      } else {
        //Okay, let's get to work - we have a table and stats to read

        foreach (DataRow _dr in _ds.Tables[0].Rows) {
          GuildEntryStat _entry = new GuildEntryStat(_dr);
          _stats.Add(_entry);
        }
      }
      return _stats.ToArray<GuildEntryStat>();
    }

  }

  public class SqlWgrExceptionRepository : SqlDataProvider, IRepository<WoWGuildRosterException> {
    #region IRepository<WoWGuildRosterException> Members
    public WoWGuildRosterException[] All() {
      String _methodName = this.ClassName + ".All()";

      this.CheckConnectionString(_methodName);
      List<WoWGuildRosterException> _entries = new List<WoWGuildRosterException>();

      try {
        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, FullyQualifiedName("DNW_WGR_GetErrors"));

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException(_methodName + " - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no entries to process
        } else {
          //Okay, let's get to work - we have a table and errors to read
          foreach (DataRow _dr in _ds.Tables[0].Rows) {
            WoWGuildRosterException _entry = new WGRGeneralError(_dr);
            _entries.Add(_entry);
          }
        }
        return _entries.ToArray<WoWGuildRosterException>();

      } catch (Exception e) {
        //
        //TODO - Uncomment this out when add in exception logging
        //
        //(new Business.Exceptions.GuildCharEntryError(e, -1, -1, entryId, _methodName)).WriteExceptionToDB();
        return null;
      }
    }

    public WoWGuildRosterException[] FindAll(Func<WoWGuildRosterException, bool> exp) {
      throw new NotImplementedException();
    }

    public WoWGuildRosterException Find(int errorId) {
      String _methodName = this.ClassName + ".Find(" + errorId.ToString() + ")";

      this.CheckConnectionString(_methodName);
      List<WoWGuildRosterException> _entries = new List<WoWGuildRosterException>();

      try {
        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, FullyQualifiedName("DNW_WGR_GetError_ByErrorID"), errorId);

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException(_methodName + " - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no entries to process
          return new WGRGeneralError();
        } else if (_ds.Tables[0].Rows.Count > 1) {
          //Too many rows returned; log the error to the database and return the first one
          //
          //TODO - Uncomment this out when add in exception logging
          //
          //(new Business.Exceptions.GuildCharEntryError(new Exception(
          //  _methodName + " - Too Many Rows Returned"),
          //  -1, -1, entryId,
          //  _methodName + " - " + _ds.Tables[0].Rows.Count.ToString() + " Rows Returned")).WriteExceptionToDB();
          return new WGRGeneralError(_ds.Tables[0].Rows[0]);
        } else {
          //Okay, let's get to work - we have a table and roster to read
          return new WGRGeneralError(_ds.Tables[0].Rows[0]);
        }

      } catch (Exception e) {
        //
        //TODO - Uncomment this out when add in exception logging
        //
        //(new Business.Exceptions.GuildCharEntryError(e, -1, -1, entryId, _methodName)).WriteExceptionToDB();
        return null;
      }
    }

    public void Delete(WoWGuildRosterException entity) {
      if (entity == null) {
        return;
      }

      int _errorID = entity.ErrorID;

      String _methodName = this.ClassName + ".Delete(" + _errorID.ToString() + ")";

      if (_errorID < 1) { return; }
      this.CheckConnectionString(_methodName);

      try {
        int update = SqlHelper.ExecuteNonQuery(ConnectionString, FullyQualifiedName("DNW_WGR_DeleteErrors_ByError"), _errorID);
        if (update > 0) {
          return;
        } else {
          return;
        }
      } catch (Exception e) {
        //
        //TODO - Uncomment this out when add in exception logging
        //
        //(new Business.Exceptions.GuildCharEntryError(e, -1, -1, entryId, _methodName)).WriteExceptionToDB();
        return;
      }
    }

    public int Save(WoWGuildRosterException entity) {
      if (entity == null) {
        return -1;
      }
      int _errorID = entity.ErrorID;

      String _methodName = this.ClassName + ".Save(" + _errorID.ToString() + ")";

      this.CheckConnectionString(_methodName);
      try {
        if (_errorID < 1) {
          //Existing entity - add
          int _newErrorID = SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_AddError",
            SqlDataUtils.ValidateDbValue(entity.LoadID),
            SqlDataUtils.ValidateDbValue(entity.RosterID),
            SqlDataUtils.ValidateDbValue(entity.CharID),
            SqlDataUtils.ValidateDbValue(entity.CharStatID),
            SqlDataUtils.ValidateDbValue(entity.ErrorTime),
            SqlDataUtils.ValidateDbValue(entity.Exception.GetType().ToString()),
            SqlDataUtils.ValidateDbValue(entity.AdditionalInfo),
            SqlDataUtils.ValidateDbValue("<b>" + entity.Exception.Message + "</b> - " + entity.Exception.StackTrace));
          return _newErrorID;
        } else {
          //Existing entity - update
          return _errorID;
        }
      } catch (Exception eExc) {
        throw eExc;
      }

    }
    #endregion
  }

  public class SqlDataLoadRepository : SqlDataProvider, IRepository<DataLoad> {

    #region IRepository<DataLoad> Members
    public DataLoad[] All() {
      String _methodName = this.ClassName + ".All()";

      this.CheckConnectionString(_methodName);

      List<DataLoad> _loads = new List<DataLoad>();

      DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, FullyQualifiedName("DNW_WGR_GetDataLoads"));

      if (_ds.Tables.Count <= 0) {
        //No tables returned? - WTF?
        throw new ApplicationException(_methodName + " - No Tables Returned");
      } else if (_ds.Tables[0].Rows.Count <= 0) {
        //No rows were returned; no rosters to process
      } else {
        //Okay, let's get to work - we have a table and loads to read

        foreach (DataRow _dr in _ds.Tables[0].Rows) {
          DataLoad _load = new DataLoad(_dr);
          _loads.Add(_load);
        }
      }
      return _loads.ToArray<DataLoad>();
    }

    public DataLoad[] FindAll(Func<DataLoad, bool> exp) {
      throw new NotImplementedException();
    }

    public DataLoad Find(int loadId) {
      String _methodName = this.ClassName + ".Find(" + loadId.ToString() + ")";

      this.CheckConnectionString(_methodName);

      try {
        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, FullyQualifiedName("DNW_WGR_GetDataLoad"), loadId);

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException(_methodName + " - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no entry to process
          return new DataLoad();
        } else if (_ds.Tables[0].Rows.Count > 1) {
          //Too many rows returned; log the error to the database and return the first one
          //
          //TODO - Uncomment this out when add in exception logging
          //
          //(new Business.Exceptions.GuildCharEntryError(new Exception(
          //  _methodName + " - Too Many Rows Returned"),
          //  -1, -1, entryId,
          //  _methodName + " - " + _ds.Tables[0].Rows.Count.ToString() + " Rows Returned")).WriteExceptionToDB();
          return new DataLoad(_ds.Tables[0].Rows[0]);
        } else {
          return new DataLoad(_ds.Tables[0].Rows[0]);
        }

      } catch (Exception e) {
        //
        //TODO - Uncomment this out when add in exception logging
        //
        //(new Business.Exceptions.GuildCharEntryError(e, -1, -1, entryId, _methodName)).WriteExceptionToDB();
        return null;
      }
    }

    public void Delete(DataLoad load) {
      if (load == null) {
        return;
      }

      int _loadId = load.Load_ID;

      String _methodName = this.ClassName + ".Delete(" + _loadId.ToString() + ")";

      if (_loadId < 1) { return; }
      this.CheckConnectionString(_methodName);

      try {
        int update = SqlHelper.ExecuteNonQuery(ConnectionString, FullyQualifiedName("DNW_WGR_DeleteDataLoad"), _loadId);
        if (update > 0) {
          return;
        } else {
          return;
        }
      } catch (Exception e) {
        //
        //TODO - Uncomment this out when add in exception logging
        //
        //(new Business.Exceptions.GuildCharEntryError(e, -1, -1, entryId, _methodName)).WriteExceptionToDB();
        return;
      }
    }

    public int Save(DataLoad load) {
      if (load == null) {
        return -1;
      }
      int _loadId = load.Load_ID;

      String _methodName = this.ClassName + ".Save(" + _loadId.ToString() + ")";

      this.CheckConnectionString(_methodName);
      try {
        if (_loadId < 1) {
          //Existing entity - add
          return (int)SqlHelper.ExecuteScalar(ConnectionString, this.FullyQualifiedName("DNW_WGR_AddDataLoad"),
            SqlDataUtils.ValidateDbValue(load.Roster_ID),
            SqlDataUtils.ValidateDbValue(load.LoadType),
            SqlDataUtils.ValidateDbValue(load.LoadStatus),
            SqlDataUtils.ValidateDbValue(load.LastCharIdLoaded),
            SqlDataUtils.ValidateDbValue(load.Load_StartedBy),
            SqlDataUtils.ValidateDbValue(load.Load_Source),
            SqlDataUtils.ValidateDbValue(load.LoadTime_Started),
            SqlDataUtils.ValidateDbValue(load.LoadTime_Ended),
            SqlDataUtils.ValidateDbValue(load.LoadCount_Adds),
            SqlDataUtils.ValidateDbValue(load.LoadCount_Updates),
            SqlDataUtils.ValidateDbValue(load.LoadCount_Skips),
            SqlDataUtils.ValidateDbValue(load.LoadCount_Errors));
        } else {
          //Existing entity - update
          int update = SqlHelper.ExecuteNonQuery(ConnectionString, this.FullyQualifiedName("DNW_WGR_UpdateDataLoad"),
            load.Load_ID,
            SqlDataUtils.ValidateDbValue(load.Roster_ID),
            SqlDataUtils.ValidateDbValue(load.LoadType),
            SqlDataUtils.ValidateDbValue(load.LoadStatus),
            SqlDataUtils.ValidateDbValue(load.LastCharIdLoaded),
            SqlDataUtils.ValidateDbValue(load.Load_StartedBy),
            SqlDataUtils.ValidateDbValue(load.Load_Source),
            SqlDataUtils.ValidateDbValue(load.LoadTime_Started),
            SqlDataUtils.ValidateDbValue(load.LoadTime_Ended),
            SqlDataUtils.ValidateDbValue(load.LoadCount_Adds),
            SqlDataUtils.ValidateDbValue(load.LoadCount_Updates),
            SqlDataUtils.ValidateDbValue(load.LoadCount_Skips),
            SqlDataUtils.ValidateDbValue(load.LoadCount_Errors));

          return load.Roster_ID;
        }
      } catch (Exception eExc) {
        throw eExc;
      }
    }
    #endregion
  }

}
